package com.roger.springcloudGreenwich.api;

import com.roger.springcloudGreenwich.bean.Film;
import com.roger.springcloudGreenwich.bean.Student;
import com.roger.springcloudGreenwich.dao.StudentDao;
import com.roger.springcloudGreenwich.service.FilmService;
import com.roger.springcloudGreenwich.service.impl.IndexOper;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by admin on 2020/3/11.
 */
@RestController
@Slf4j
public class FilmController {
    //private final static Logger logger = LoggerFactory.getLogger(FilmController.class);
    @Autowired
    private FilmService filmService;
    @Autowired
    private IndexOper indexOper;

    @Autowired
    private StudentDao studentDao;

//    @Autowired
//    private RestHighLevelClient client;

    @PostMapping("/saveFilm")
    public Object saveFilm(@RequestBody Film film){
        String uuid = UUID.randomUUID().toString();
        film.setUuid(uuid);
        filmService.save(film);
        return "success";
    }

    @PostMapping("/findFilm")
    public Object findFilm(@RequestBody Film film){
        Object obj = filmService.findByTitle(film.getName());
        return obj;
    }

    @GetMapping("/insert/{indexName}")
    public String insert(@PathVariable String indexName){
        indexOper.addIndex(indexName);
        return "success";
    }

    @GetMapping("/delete/{indexName}")
    public String delete(@PathVariable String indexName){
        indexOper.deleteIndex(indexName);
        return "success";
    }

    @GetMapping("/testLog")
    public String test(){
        log.info(System.currentTimeMillis() + "");
        return System.currentTimeMillis() + "";
    }

    @GetMapping("/saveStudents")
    public String insertLists() throws Exception {
        long begin = System.currentTimeMillis();
        List<Student> list = this.getStudents();
        studentDao.bulkCreate("students","doc",list);
        long end = System.currentTimeMillis();
        String time = (end - begin) + "";
        return time;
    }

    @GetMapping("/saveStudents2")
    public String insertLists2() throws Exception {
        long begin = System.currentTimeMillis();
        List<Student> list = this.getStudents();
        studentDao.highLevelBulkCreate("students","doc",list);
        long end = System.currentTimeMillis();
        String time = (end - begin) + "";
        return time;
    }

    private List<Student> getStudents(){
        List<Student> list = new ArrayList<>();
        Student student;
        for(int i = 0; i < 10000; i++){
            student = new Student();
            student.setId(UUID.randomUUID().toString());
            student.setAge(12);
            student.setAddr("home" + i);
            student.setName("tom" + (new Date()));
            list.add(student);
        }
        return list;
    }

}
